package cn.yunhe.java.util;

import cn.yunhe.java.dto.MerchantDTO;
import cn.yunhe.java.service.IMerchantService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huiminpay.common.cache.util.ApplicationContextHelper;
import com.huiminpay.common.cache.util.EncryptUtil;
import com.huiminpay.common.cache.util.LoginUser;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/***
 * 获取当前登录用户信息
 * 前端配置token，后续每次请求并通过Header方式发送至后端
 */
public class SecurityUtil {

	//测试使用
//	public static Long getMerchantId() {
//		HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//		String jsonToken = request.getHeader("authorization");
//		if (StringUtils.isEmpty(jsonToken) || !jsonToken.startsWith("Bearer ")) {
//			throw new RuntimeException("token is not as expected");
//		}
//		jsonToken = jsonToken.substring(7);
//		jsonToken = EncryptUtil.decodeUTF8StringBase64(jsonToken);
//		JSONObject jsonObject = JSON.parseObject(jsonToken);
//		return jsonObject.getLong("merchantId");
//	}


	public static LoginUser getUser() {
		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		//jsonToken转对象
		if (servletRequestAttributes != null) {
			HttpServletRequest request = servletRequestAttributes.getRequest();
//			Object jwt = request.getAttribute("jsonToken");
//			getHeader需要前端传jsonToken
			Object jwt = request.getHeader("jsonToken");
			byte[] jwtArray = EncryptUtil.decodeBase64((String)jwt);
			String jwtString = new String(jwtArray, StandardCharsets.UTF_8);
			LoginUser user = JSONObject.parseObject(jwtString,LoginUser.class);//使用parseObject方法转为login user对象

			return user;
		}
		return new LoginUser();
	}

	/**
	 * 通过token获取所得用户
	 * @return
	 */
	public static Long getMerchantId(IMerchantService merchantService){
//		IMerchantService merchantService = ApplicationContextHelper.getBean(IMerchantService.class);
		//获取租户id，然后根据租户id查询租户信息
		MerchantDTO merchant = merchantService.queryMerchantByTenantId(getUser().getTenantId());
		System.out.println(merchant);
		Long merchantId = null;
		if(merchant!=null){
			merchantId = merchant.getId();
		}
		return merchantId;
	}

	/**
	 * 转换明文jsonToken为用户对象
	 * @param token
	 * @return
	 */
	public static LoginUser convertTokenToLoginUser(String token) {
		token = EncryptUtil.decodeUTF8StringBase64(token);
		LoginUser user = new LoginUser();
		JSONObject jsonObject = JSON.parseObject(token);
		String payload = jsonObject.getString("payload");
		Map<String, Object> payloadMap = JSON.parseObject(payload, Map.class);
		user.setPayload(payloadMap);
		user.setClientId(jsonObject.getString("client_id"));
		user.setMobile(jsonObject.getString("mobile"));
		user.setUsername(jsonObject.getString("user_name"));
		return user;
	}

}
